package com.ruoyi.production.service.impl.stroma;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.production.domain.stroma.StromaAmount;
import com.ruoyi.production.domain.stroma.StromaPurchase;
import com.ruoyi.production.domain.stroma.StromaAmount;
import com.ruoyi.production.mapper.stroma.StromaAmountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.production.mapper.stroma.StromaPurchaseMapper;
import com.ruoyi.production.domain.stroma.StromaPurchase;
import com.ruoyi.production.service.stroma.IStromaPurchaseService;

/**
 * 基质购买信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@Service
public class StromaPurchaseServiceImpl implements IStromaPurchaseService
{
    @Autowired
    private StromaPurchaseMapper stromaPurchaseMapper;
    @Autowired
    private StromaAmountMapper stromaAmountMapper;

    /**
     * 查询基质购买信息
     *
     * @param stromaPurchaseId 基质购买信息ID
     * @return 基质购买信息
     */
    @Override
    public StromaPurchase selectStromaPurchaseById(Long stromaPurchaseId)
    {
        return stromaPurchaseMapper.selectStromaPurchaseById(stromaPurchaseId);
    }

    /**
     * 查询基质购买信息列表
     *
     * @param stromaPurchase 基质购买信息
     * @return 基质购买信息
     */
    @Override
    public List<StromaPurchase> selectStromaPurchaseList(StromaPurchase stromaPurchase)
    {
        return stromaPurchaseMapper.selectStromaPurchaseList(stromaPurchase);
    }

    /**
     * 新增基质购买信息
     *
     * @param stromaPurchase 基质购买信息
     * @return 结果
     */
    @Override
    public int insertStromaPurchase(StromaPurchase stromaPurchase)
    {
        return stromaPurchaseMapper.insertStromaPurchase(stromaPurchase);
    }

    /**
     * 修改基质购买信息
     *
     * @param stromaPurchase 基质购买信息
     * @return 结果
     */
    @Override
    public int updateStromaPurchase(StromaPurchase stromaPurchase)
    {
        Long stromaPurchaseId = stromaPurchase.getStromaPurchaseId();
        //数据库存储的数据
        StromaPurchase PurchaseDB = selectStromaPurchaseById(stromaPurchaseId);
        //数据库存储的原数据的总量
        StromaAmount originAmounts = new StromaAmount();
        originAmounts.setStromaKindId(PurchaseDB.getStromaKindId());
        originAmounts.setStromaTypeId(PurchaseDB.getStromaTypeId());
        originAmounts.setOriginCompany(PurchaseDB.getOriginCompany());
        originAmounts.setBrand(PurchaseDB.getBrand());
        List<StromaAmount> amounts = stromaAmountMapper.selectStromaAmountList(originAmounts);
        StromaAmount originAmount = amounts.get(0);
        if (PurchaseDB.getOriginCompany().equals(stromaPurchase.getOriginCompany()) &&
                PurchaseDB.getStromaKindId().equals(stromaPurchase.getStromaKindId()) &&
                PurchaseDB.getStromaTypeId().equals(stromaPurchase.getStromaTypeId()) &&
                PurchaseDB.getBrand().equals(stromaPurchase.getBrand())
        ) {
            originAmount.setStorageAmount(originAmount.getStorageAmount()+stromaPurchase.getPurchaseAmount()-PurchaseDB.getPurchaseAmount());
            stromaAmountMapper.updateStromaAmount(originAmount);
        }else {
            StromaAmount newAmounts = new StromaAmount();
            newAmounts.setStromaTypeId(stromaPurchase.getStromaTypeId());
            newAmounts.setStromaKindId(stromaPurchase.getStromaKindId());
            newAmounts.setOriginCompany(stromaPurchase.getOriginCompany());
            newAmounts.setBrand(stromaPurchase.getBrand());
            List<StromaAmount> amounts1 = stromaAmountMapper.selectStromaAmountList(newAmounts);
            if (StringUtils.isEmpty(amounts1)){
                newAmounts.setStorageAmount(stromaPurchase.getPurchaseAmount());
                stromaAmountMapper.insertStromaAmount(newAmounts);
            }else {
                StromaAmount newAmount = amounts1.get(0);
                newAmount.setStorageAmount(newAmount.getStorageAmount()+stromaPurchase.getPurchaseAmount());
                stromaAmountMapper.updateStromaAmount(newAmount);
            }
            originAmount.setStorageAmount(originAmount.getStorageAmount()-PurchaseDB.getPurchaseAmount());
            stromaAmountMapper.updateStromaAmount(originAmount);
        }
        return stromaPurchaseMapper.updateStromaPurchase(stromaPurchase);
    }

    /**
     * 批量删除基质购买信息
     *
     * @param stromaPurchaseIds 需要删除的基质购买信息ID
     * @return 结果
     */
    @Override
    public int deleteStromaPurchaseByIds(Long[] stromaPurchaseIds)
    {
        for (Long stromaPurchaseId : stromaPurchaseIds) {
            StromaPurchase stromaPurchase = selectStromaPurchaseById(stromaPurchaseId);
            StromaAmount stromaAmount = new StromaAmount();
            stromaAmount.setStromaKindId(stromaPurchase.getStromaKindId());
            stromaAmount.setOriginCompany(stromaPurchase.getOriginCompany());
            stromaAmount.setStromaTypeId(stromaPurchase.getStromaTypeId());
            stromaAmount.setBrand(stromaPurchase.getBrand());
            List<StromaAmount> amounts = stromaAmountMapper.selectStromaAmountList(stromaAmount);
            StromaAmount stromaAmount1 = amounts.get(0);
            stromaAmount1.setStorageAmount(stromaAmount1.getStorageAmount() - stromaPurchase.getPurchaseAmount());
            stromaAmountMapper.updateStromaAmount(stromaAmount1);
        }
        return stromaPurchaseMapper.deleteStromaPurchaseByIds(stromaPurchaseIds);
    }

    /**
     * 删除基质购买信息信息
     *
     * @param stromaPurchaseId 基质购买信息ID
     * @return 结果
     */
    @Override
    public int deleteStromaPurchaseById(Long stromaPurchaseId)
    {
        StromaPurchase stromaPurchase = selectStromaPurchaseById(stromaPurchaseId);
        StromaAmount stromaAmount = new StromaAmount();
        stromaAmount.setStromaKindId(stromaPurchase.getStromaKindId());
        stromaAmount.setStromaTypeId(stromaPurchase.getStromaTypeId());
        stromaAmount.setOriginCompany(stromaPurchase.getOriginCompany());
        stromaAmount.setBrand(stromaPurchase.getBrand());
        List<StromaAmount> amounts = stromaAmountMapper.selectStromaAmountList(stromaAmount);
        StromaAmount stromaAmount1 = amounts.get(0);
        stromaAmount1.setStorageAmount(stromaAmount1.getStorageAmount() - stromaPurchase.getPurchaseAmount());
        stromaAmountMapper.updateStromaAmount(stromaAmount1);
        return stromaPurchaseMapper.deleteStromaPurchaseById(stromaPurchaseId);
    }


    /**
     * 插入种子购买表更新种子量表
     *
     * @param stromaPurchase
     * @return
     */
    @Override
    public int insertPurchaseUpdateAmount(StromaPurchase stromaPurchase) {
        StromaAmount stromaAmount = new StromaAmount();
        stromaAmount.setStromaKindId(stromaPurchase.getStromaKindId());
        stromaAmount.setBrand(stromaPurchase.getBrand());
        stromaAmount.setStromaTypeId(stromaPurchase.getStromaTypeId());
        stromaAmount.setOriginCompany(stromaPurchase.getOriginCompany());
        List<StromaAmount> stromaAmounts = stromaAmountMapper.selectStromaAmountList(stromaAmount);
        stromaAmount.setStorageAmount(stromaPurchase.getPurchaseAmount());
        if (!StringUtils.isEmpty(stromaAmounts)) {
            StromaAmount amount = stromaAmounts.get(0);
            amount.setStorageAmount(amount.getStorageAmount() + stromaPurchase.getPurchaseAmount());
            stromaAmountMapper.updateStromaAmount(amount);
        } else {
            stromaAmountMapper.insertStromaAmount(stromaAmount);
        }
        return insertStromaPurchase(stromaPurchase);
    }
}
